package com.sg.service.biz.pbff.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.pbff.req.BffQueryTimerTaskPlanDetailReqDto;
import com.sg.dto.biz.pbff.req.BffQueryTimerTaskPlanListReqDto;
import com.sg.dto.biz.pbff.res.BffQueryTimerTaskPlanDetailRespDto;
import com.sg.dto.biz.pbff.res.BffQueryTimerTaskPlanListRespDto;
import com.sg.service.biz.pbff.TaskGenService;
import com.wicket.okrapp.integration.TaskGenService;
import com.wicket.okrapp.integration.dto.*;
import com.wicket.okrframework.integration.BaseService;
import com.wicket.okrframework.integration.InductionRecordService;
import com.wicket.okrframework.integration.dto.*;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class TaskGenServiceImpl implements TaskGenService {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    com.sg.service.biz.app.TaskGenService fwAppTaskGenClient;

    @Resource
    com.sg.service.biz.framework.InductionRecordService fwBaseInductionRecordClient;

    @Resource
    com.sg.service.biz.framework.BaseService fwBaseBaseClient;

    /**
     * B3-2查询定时任务调度列表(管理)(平台)[3313]
     * gen by moon at 11/22/2022, 8:49:58 PM
     */
    @Trace(operationName = "B3-2查询定时任务调度列表(管理)(平台)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQueryTimerTaskPlanListRespDto queryTimerTaskPlanList(BffQueryTimerTaskPlanListReqDto reqDto) {


        QueryTimerTaskPlanListRespDto queryTimerTaskPlanListRespDto_1 = null;
        BatchQueryInductionRecordComRespDto batchQueryInductionRecordComRespDto_1 = null;
        BatchQuerySpaceComRespDto batchQuerySpaceComRespDto_1 = null;
//步骤0: D3-2查询定时任务调度列表 - queryTimerTaskPlanList
        QueryTimerTaskPlanListRespDto queryTimerTaskPlanListRespDto = null;
        QueryTimerTaskPlanListReqDto queryTimerTaskPlanListReqDto = new QueryTimerTaskPlanListReqDto();
        if (reqDto != null) {
            queryTimerTaskPlanListReqDto.setTaskDispatchTypeCode(reqDto.getTaskDispatchTypeCode());//SimpleFieldAssign//sourceId:128210_1
            queryTimerTaskPlanListReqDto.setDispatchActionTypeCode(reqDto.getDispatchActionTypeCode());//SimpleFieldAssign//sourceId:128211_1
            queryTimerTaskPlanListReqDto.setObjectTypeCode(reqDto.getObjectTypeCode());//SimpleFieldAssign//sourceId:128212_1
            queryTimerTaskPlanListReqDto.setIsCompensateTask(reqDto.getIsCompensateTask());//SimpleFieldAssign//sourceId:128215_1
            queryTimerTaskPlanListReqDto.setIsComplete(reqDto.getIsComplete());//SimpleFieldAssign//sourceId:128216_1
            queryTimerTaskPlanListReqDto.setIsRoutineDispatchAction(reqDto.getIsRoutineDispatchAction());//SimpleFieldAssign//sourceId:448561_1
            queryTimerTaskPlanListReqDto.setBizAppCode(reqDto.getBizAppCode());//SimpleFieldAssign//sourceId:388826_1
            queryTimerTaskPlanListReqDto.setBizSceneCode(reqDto.getBizSceneCode());//SimpleFieldAssign//sourceId:388827_1
            queryTimerTaskPlanListReqDto.setSpaceId(reqDto.getSpaceId());//SimpleFieldAssign//sourceId:128217_1
        }

        /*D3-2查询定时任务调度列表[3311]   */
        Assert.isNull(queryTimerTaskPlanListReqDto.getTaskDispatchTypeCode(), "B3-2查询定时任务调度列表(管理)(平台)-D3-2查询定时任务调度列表-任务调度类型编码不能为空", false);
        queryTimerTaskPlanListRespDto = fwAppTaskGenClient.queryTimerTaskPlanList(queryTimerTaskPlanListReqDto).getData();


        queryTimerTaskPlanListRespDto_1 = queryTimerTaskPlanListRespDto;

//步骤1: D1-3批量查询就职记录(管理)(公共) - batchQueryInductionRecordCom
        BatchQueryInductionRecordComRespDto batchQueryInductionRecordComRespDto = null;
        if (queryTimerTaskPlanListRespDto != null) {
            BatchQueryInductionRecordComReqDto batchQueryInductionRecordComReqDto = new BatchQueryInductionRecordComReqDto();
            if (queryTimerTaskPlanListRespDto != null) {
                batchQueryInductionRecordComReqDto.setOrgInductionRecordList(queryTimerTaskPlanListRespDto.getTimerTaskPlanList().stream().map(item -> item.getCreateInductionId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:394367_1
            }

            /*D1-3批量查询就职记录(管理)(公共)[5024]   */

            batchQueryInductionRecordComRespDto = fwBaseInductionRecordClient.batchQueryInductionRecordCom(batchQueryInductionRecordComReqDto).getData();


            batchQueryInductionRecordComRespDto_1 = batchQueryInductionRecordComRespDto;
        }

//步骤2: D1-1批量查询空间(公共) - batchQuerySpaceCom
        BatchQuerySpaceComRespDto batchQuerySpaceComRespDto = null;
        if (queryTimerTaskPlanListRespDto != null) {
            BatchQuerySpaceComReqDto batchQuerySpaceComReqDto = new BatchQuerySpaceComReqDto();
            if (queryTimerTaskPlanListRespDto != null) {
                batchQuerySpaceComReqDto.setSpaceList(queryTimerTaskPlanListRespDto.getTimerTaskPlanList().stream().map(item -> item.getSpaceId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:128553_1
            }

            /*D1-1批量查询空间(公共)[2149]   */

            batchQuerySpaceComRespDto = fwBaseBaseClient.batchQuerySpaceCom(batchQuerySpaceComReqDto).getData();


            batchQuerySpaceComRespDto_1 = batchQuerySpaceComRespDto;
        }

        BffQueryTimerTaskPlanListRespDto retData = new BffQueryTimerTaskPlanListRespDto();
        //数据集融合  MoreListToOneSource
        if (queryTimerTaskPlanListRespDto_1 != null && !CollectionUtil.isEmpty(queryTimerTaskPlanListRespDto_1.getTimerTaskPlanList())) {
            for (TimerTaskPlanDto timerTaskPlanDto : queryTimerTaskPlanListRespDto_1.getTimerTaskPlanList()) {
                BffTimerTaskPlanDto retElm = new BffTimerTaskPlanDto();
                if (queryTimerTaskPlanListRespDto_1 != null) {
                    retElm.setTimingTaskDispatchId(timerTaskPlanDto.getTimingTaskDispatchId());//SimpleFieldAssign//sourceId:65261_2
                    retElm.setTimingTaskDispatchName(timerTaskPlanDto.getTimingTaskDispatchName());//SimpleFieldAssign//sourceId:65262_2
                    retElm.setObjectId(timerTaskPlanDto.getObjectId());//SimpleFieldAssign//sourceId:65264_2
                    retElm.setIsRoutineDispatchAction(timerTaskPlanDto.getIsRoutineDispatchAction());//SimpleFieldAssign//sourceId:119277_2
                    retElm.setIsComplete(timerTaskPlanDto.getIsComplete());//SimpleFieldAssign//sourceId:65265_2
                    retElm.setCreateInductionId(timerTaskPlanDto.getCreateInductionId());//SimpleFieldAssign//sourceId:65266_2
                    retElm.setCreateTime(timerTaskPlanDto.getCreateTime());//SimpleFieldAssign//sourceId:65267_2
                    retElm.setSpaceId(timerTaskPlanDto.getSpaceId());//SimpleFieldAssign//sourceId:65268_2
                }
                retData.getTimerTaskPlanList().add(retElm);
                if (batchQueryInductionRecordComRespDto_1 != null) {
                    for (OrgInductionRecordDto orgInductionRecordDto : batchQueryInductionRecordComRespDto_1.getOrgInductionRecordList()) {
                        if (orgInductionRecordDto.getInductionRecordId().equals(timerTaskPlanDto.getCreateInductionId())) {
                            if (batchQueryInductionRecordComRespDto_1 != null) {
                                retElm.setUserId(orgInductionRecordDto.getUserId());//SimpleFieldAssign//sourceId:110858_2
                                retElm.setUserFace(orgInductionRecordDto.getUserFace());//SimpleFieldAssign//sourceId:110859_2
                                retElm.setIdCardName(orgInductionRecordDto.getIdCardName());//SimpleFieldAssign//sourceId:110860_2
                                retElm.setNickName(orgInductionRecordDto.getNickName());//SimpleFieldAssign//sourceId:110862_2
                            }
                        }
                    }
                }

                if (batchQuerySpaceComRespDto_1 != null) {
                    for (FrameworkSpaceDto frameworkSpaceDto : batchQuerySpaceComRespDto_1.getSpaceList()) {
                        if (frameworkSpaceDto.getSpaceId().equals(timerTaskPlanDto.getSpaceId())) {
                            if (batchQuerySpaceComRespDto_1 != null) {
                                retElm.setAbbreviation(frameworkSpaceDto.getAbbreviation());//SimpleFieldAssign//sourceId:65288_2
                            }
                        }
                    }
                }

            }
        }//sourceId:128227_1


        retData.setPageNum(queryTimerTaskPlanListRespDto.getPageNum());
        retData.setPageSize(queryTimerTaskPlanListRespDto.getPageSize());
        retData.setTotal(queryTimerTaskPlanListRespDto.getTotalNum());
        retData.setIsLastPage((Math.ceil(Double.valueOf(queryTimerTaskPlanListRespDto.getTotalNum()) / Double.valueOf(queryTimerTaskPlanListRespDto.getPageSize()))) <= retData.getPageNum());


        return retData;
    }

    /**
     * B3-2查询定时任务调度详情(管理)(平台)[3312]
     * gen by moon at 5/15/2023, 12:16:16 AM
     */
    @Trace(operationName = "B3-2查询定时任务调度详情(管理)(平台)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQueryTimerTaskPlanDetailRespDto queryTimerTaskPlanDetail(BffQueryTimerTaskPlanDetailReqDto reqDto) {


        QueryTimerTaskPlanDetailRespDto queryTimerTaskPlanDetailRespDto_1 = null;
        QueryInductionRecordInfoDetailComRespDto queryInductionRecordInfoDetailComRespDto_1 = null;
        QueryInductionRecordInfoDetailComRespDto queryInductionRecordInfoDetailComRespDto_3 = null;
        QuerySpaceDetailComRespDto querySpaceDetailComRespDto_1 = null;
//步骤0: D3-2查询定时任务调度详情 - queryTimerTaskPlanDetail
        QueryTimerTaskPlanDetailRespDto queryTimerTaskPlanDetailRespDto = null;
        QueryTimerTaskPlanDetailReqDto queryTimerTaskPlanDetailReqDto = new QueryTimerTaskPlanDetailReqDto();
        if (reqDto != null) {
            queryTimerTaskPlanDetailReqDto.setTimingTaskDispatchId(reqDto.getTimingTaskDispatchId());//SimpleFieldAssign//sourceId:128305_1
        }

        /*D3-2查询定时任务调度详情[3310]   */

        queryTimerTaskPlanDetailRespDto = fwAppTaskGenClient.queryTimerTaskPlanDetail(queryTimerTaskPlanDetailReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


        queryTimerTaskPlanDetailRespDto_1 = queryTimerTaskPlanDetailRespDto;

//步骤1: D1-3查询个人就职记录信息详情（公共） - queryInductionRecordInfoDetailCom
        QueryInductionRecordInfoDetailComRespDto queryInductionRecordInfoDetailComRespDto = null;
        if (queryTimerTaskPlanDetailRespDto != null) {
            QueryInductionRecordInfoDetailComReqDto queryInductionRecordInfoDetailComReqDto = new QueryInductionRecordInfoDetailComReqDto();
            if (queryTimerTaskPlanDetailRespDto != null) {
                queryInductionRecordInfoDetailComReqDto.setInductionRecordId(queryTimerTaskPlanDetailRespDto.getCreateInductionId());//SimpleFieldAssign//sourceId:128339_1
            }

            /*D1-3查询创建人个人就职记录信息详情（公共）[715]   */
            Assert.isNull(queryInductionRecordInfoDetailComReqDto.getInductionRecordId(), "B3-2查询定时任务调度详情(管理)(平台)-D1-3查询创建人个人就职记录信息详情（公共）-就职记录ID不能为空", false);
            queryInductionRecordInfoDetailComRespDto = fwBaseInductionRecordClient.queryInductionRecordInfoDetailCom(queryInductionRecordInfoDetailComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


            queryInductionRecordInfoDetailComRespDto_1 = queryInductionRecordInfoDetailComRespDto;
        }

//步骤2: D1-3查询个人就职记录信息详情（公共） - queryInductionRecordInfoDetailCom
        QueryInductionRecordInfoDetailComRespDto queryInductionRecordInfoDetailComRespDto_2 = null;
        if (queryTimerTaskPlanDetailRespDto != null) {
            QueryInductionRecordInfoDetailComReqDto queryInductionRecordInfoDetailComReqDto_1 = new QueryInductionRecordInfoDetailComReqDto();
            if (queryTimerTaskPlanDetailRespDto != null) {
                queryInductionRecordInfoDetailComReqDto_1.setInductionRecordId(queryTimerTaskPlanDetailRespDto.getOperationInductionId());//SimpleFieldAssign//sourceId:128347_1
            }

            /*D1-3查询操作人个人就职记录信息详情（公共）[715]   */
            Assert.isNull(queryInductionRecordInfoDetailComReqDto_1.getInductionRecordId(), "B3-2查询定时任务调度详情(管理)(平台)-D1-3查询操作人个人就职记录信息详情（公共）-就职记录ID不能为空", false);
            queryInductionRecordInfoDetailComRespDto_2 = fwBaseInductionRecordClient.queryInductionRecordInfoDetailCom(queryInductionRecordInfoDetailComReqDto_1).getData()/*vcase invoke 跨dubbo调用;*/;


            queryInductionRecordInfoDetailComRespDto_3 = queryInductionRecordInfoDetailComRespDto_2;
        }

//步骤3: D1-2查空间详情(公共) - querySpaceDetailCom
        QuerySpaceDetailComRespDto querySpaceDetailComRespDto = null;
        if (queryTimerTaskPlanDetailRespDto != null) {
            QuerySpaceDetailComReqDto querySpaceDetailComReqDto = new QuerySpaceDetailComReqDto();
            if (queryTimerTaskPlanDetailRespDto != null) {
                querySpaceDetailComReqDto.setSpaceId(queryTimerTaskPlanDetailRespDto.getSpaceId());//SimpleFieldAssign//sourceId:128355_1
            }

            /*D1-2查空间详情(公共)[402]   */
            Assert.isNull(querySpaceDetailComReqDto.getSpaceId(), "B3-2查询定时任务调度详情(管理)(平台)-D1-2查空间详情(公共)-空间ID不能为空", false);
            querySpaceDetailComRespDto = fwBaseBaseClient.querySpaceDetailCom(querySpaceDetailComReqDto).getData()/*vcase invoke 跨dubbo调用;*/;


            querySpaceDetailComRespDto_1 = querySpaceDetailComRespDto;
        }

        BffQueryTimerTaskPlanDetailRespDto retData = new BffQueryTimerTaskPlanDetailRespDto();
        if (queryTimerTaskPlanDetailRespDto_1 != null) {
            retData.setTimingTaskDispatchId(queryTimerTaskPlanDetailRespDto_1.getTimingTaskDispatchId());//SimpleFieldAssign//sourceId:128323_1
            retData.setTimingTaskDispatchName(queryTimerTaskPlanDetailRespDto_1.getTimingTaskDispatchName());//SimpleFieldAssign//sourceId:128324_1
            retData.setIsComplete(queryTimerTaskPlanDetailRespDto_1.getIsComplete());//SimpleFieldAssign//sourceId:128325_1
            retData.setIsCompensateTask(queryTimerTaskPlanDetailRespDto_1.getIsCompensateTask());//SimpleFieldAssign//sourceId:128326_1
            retData.setIsAppointSimpleMq(queryTimerTaskPlanDetailRespDto_1.getIsAppointSimpleMq());//SimpleFieldAssign//sourceId:540105_1
            retData.setIsRoutineDispatchAction(queryTimerTaskPlanDetailRespDto_1.getIsRoutineDispatchAction());//SimpleFieldAssign//sourceId:441165_1
            retData.setTaskDispatchTypeCode(queryTimerTaskPlanDetailRespDto_1.getTaskDispatchTypeCode());//SimpleFieldAssign//sourceId:128327_1
            retData.setDispatchActionTypeCode(queryTimerTaskPlanDetailRespDto_1.getDispatchActionTypeCode());//SimpleFieldAssign//sourceId:128328_1
            retData.setObjectTypeCode(queryTimerTaskPlanDetailRespDto_1.getObjectTypeCode());//SimpleFieldAssign//sourceId:128329_1
            retData.setStandbyField1(queryTimerTaskPlanDetailRespDto_1.getStandbyField1());//SimpleFieldAssign//sourceId:437745_1
            retData.setObjectId(queryTimerTaskPlanDetailRespDto_1.getObjectId());//SimpleFieldAssign//sourceId:128330_1
            retData.setAddRuleInfoTypeCode(queryTimerTaskPlanDetailRespDto_1.getAddRuleInfoTypeCode());//SimpleFieldAssign//sourceId:128331_1
            retData.setAddRuleInfoId(queryTimerTaskPlanDetailRespDto_1.getAddRuleInfoId());//SimpleFieldAssign//sourceId:128332_1
            retData.setAutoPlanTime(queryTimerTaskPlanDetailRespDto_1.getAutoPlanTime());//SimpleFieldAssign//sourceId:128333_1
            retData.setBizAppCode(queryTimerTaskPlanDetailRespDto_1.getBizAppCode());//SimpleFieldAssign//sourceId:412563_1
            retData.setBizSceneCode(queryTimerTaskPlanDetailRespDto_1.getBizSceneCode());//SimpleFieldAssign//sourceId:412564_1
            retData.setCreateInductionId(queryTimerTaskPlanDetailRespDto_1.getCreateInductionId());//SimpleFieldAssign//sourceId:128335_1
            retData.setCreateTime(queryTimerTaskPlanDetailRespDto_1.getCreateTime());//SimpleFieldAssign//sourceId:128337_1
            retData.setOperationInductionId(queryTimerTaskPlanDetailRespDto_1.getOperationInductionId());//SimpleFieldAssign//sourceId:128334_1
            retData.setOperateTime(queryTimerTaskPlanDetailRespDto_1.getOperateTime());//SimpleFieldAssign//sourceId:128336_1
            retData.setSpaceId(queryTimerTaskPlanDetailRespDto_1.getSpaceId());//SimpleFieldAssign//sourceId:128338_1
            retData.setAppId(queryTimerTaskPlanDetailRespDto_1.getAppId());//SimpleFieldAssign//sourceId:437746_1
        }
        if (queryInductionRecordInfoDetailComRespDto_1 != null) {
            retData.setUserId(queryInductionRecordInfoDetailComRespDto_1.getUserId());//SimpleFieldAssign//sourceId:128457_1
            retData.setUserFace(queryInductionRecordInfoDetailComRespDto_1.getUserFace());//SimpleFieldAssign//sourceId:128458_1
            retData.setIdCardName(queryInductionRecordInfoDetailComRespDto_1.getIdCardName());//SimpleFieldAssign//sourceId:128460_1
            retData.setNickName(queryInductionRecordInfoDetailComRespDto_1.getNickName());//SimpleFieldAssign//sourceId:128461_1
        }
        if (queryInductionRecordInfoDetailComRespDto_3 != null) {
            retData.setOperationName(queryInductionRecordInfoDetailComRespDto_3.getIdCardName());//SimpleFieldAssign//sourceId:128474_1
            retData.setOperationNickName(queryInductionRecordInfoDetailComRespDto_3.getNickName());//SimpleFieldAssign//sourceId:128475_1
        }
        if (querySpaceDetailComRespDto_1 != null) {
            retData.setAbbreviation(querySpaceDetailComRespDto_1.getAbbreviation());//SimpleFieldAssign//sourceId:128459_1
        }


        return retData;
    }
    //
}
